package com.spdzen.service.impl;

import com.spdzen.model.SysUser;
import com.github.pagehelper.PageHelper;
import com.spdzen.mapper.SysUserMapper;
import com.spdzen.service.SysUserRoleService;
import com.spdzen.service.SysUserService;
import com.spdzen.support.PageNumParam;
import com.spdzen.support.PageOffsetParam;
import com.spdzen.support.PageUtils;
import com.spdzen.support.PasswordUtils;
import com.spdzen.support.SimplePage;
import com.spdzen.support.SimpleServiceImpl;
import com.spdzen.support.exception.ResultCodeException;
import com.spdzen.support.result.BizResultCode;
import com.spdzen.support.result.ResultCode;
import com.spdzen.vo.SysUserAdminVO;
import com.spdzen.vo.search.SysUserAdminSearch;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统用户 服务实现类
 */
@Service("sysUserService")
public class SysUserServiceImpl extends SimpleServiceImpl<SysUserMapper, SysUser> implements SysUserService {
	@Resource
	private SysUserRoleService sysUserRoleService;
	
	@Override
	public SimplePage<SysUserAdminVO> selectPage(PageOffsetParam param, SysUserAdminSearch search) {
		return selectPage(PageUtils.toPageNumParam(param), search);
	}

	@Override
	public SimplePage<SysUserAdminVO> selectPage(PageNumParam param, SysUserAdminSearch search) {
		int pageNum = PageUtils.checkPageNum(param.getPageNum());
		int pageSize = PageUtils.checkPageSize(param.getPageSize());

		PageHelper.startPage(pageNum, pageSize);

		List<SysUserAdminVO> rows = baseMapper.selectAdminVO(search);

		return PageUtils.toSimplePage(rows);
	}

	@Override
	@Transactional(rollbackFor = { Exception.class })
	public void inserIfPermission(SysUser entity) throws Exception {
		entity.setPassword(PasswordUtils.encrypt(entity.getPassword()));		
		baseMapper.insert(entity);
		SysUser record = new SysUser();
		record.setLoginName(entity.getLoginName());
		if (selectCount(record) > 1) {
			throw new ResultCodeException("用户登录名已存在！");
		}
	}

	@Override
	public void updateIfPermission(SysUser entity) {
		entity.setLoginName(null);
		entity.setPassword(null);

		baseMapper.updateById(entity);
	}
	
	@Override
	@Transactional(rollbackFor = { Exception.class })
	public void deleteIfPermission(Long id) {
		baseMapper.deleteById(id);
		sysUserRoleService.deleteByUser(id);
	}
	
	@Override
	public void changePassword(Long id, String password) {
		String enpwd = PasswordUtils.encrypt(password);
		SysUser record = new SysUser();
		record.setId(id);
		record.setPassword(enpwd);
		
		baseMapper.updateById(record);
	}
	
	@Override
	public ResultCode login(String loginName, String password) {
		SysUser record = new SysUser();
		record.setLoginName(loginName);
		
		record = baseMapper.selectOne(record);
		if(record == null) {
			return BizResultCode.error("用户不存在或密码不正确！");
		}
		
		if(!PasswordUtils.compare(password, record.getPassword())){
			return BizResultCode.error("用户不存在或密码不正确！");
		}
		
		return BizResultCode.success();
	}
	
	@Override
	public SysUser selectByLoginName(String loginName) {
		if(StringUtils.isBlank(loginName)){
			return null;
		}
		
		SysUser record = new SysUser();
		record.setLoginName(loginName);
		
		return baseMapper.selectOne(record);
	}
	
	@Override
	public void updateLogin(Long id, String ip) {
		SysUser record = new SysUser();
		record.setId(id);
		record.setLastLoginIp(ip);
		
		baseMapper.updateLogin(record);
	}
}
